<%-- web/admin/reset-password.jsp --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.bookmanager.model.User" %>
<%
    // 检查管理员权限
    User currentUser = (User) session.getAttribute("user");
    if (currentUser == null || !"admin".equals(currentUser.getRole())) {
        response.sendRedirect("../login.jsp");
        return;
    }

    String userId = request.getParameter("userId");
    String username = request.getParameter("username");

    if (userId == null || username == null) {
        response.sendRedirect("user-management");
        return;
    }
%>
<html>
<head>
    <title>重置密码 - 图书管理系统</title>
    <style>
        /* 使用与 user-edit.jsp 相同的样式 */
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Segoe UI', sans-serif; background: #f5f7fa; color: #333; }
        .header { background: white; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 0 20px; }
        .nav-bar { display: flex; justify-content: space-between; align-items: center; max-width: 1200px; margin: 0 auto; height: 70px; }
        .logo { font-size: 1.5rem; font-weight: bold; color: #667eea; }
        .admin-info { display: flex; align-items: center; gap: 15px; }
        .admin-badge { background: #28a745; color: white; padding: 4px 8px; border-radius: 12px; font-size: 0.8rem; }
        .logout-btn { padding: 8px 16px; background: #dc3545; color: white; text-decoration: none; border-radius: 5px; font-size: 0.9rem; }
        .container { max-width: 500px; margin: 30px auto; padding: 0 20px; }
        .page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px; }
        .page-title { font-size: 1.8rem; color: #333; }
        .form-card { background: white; padding: 30px; border-radius: 10px; box-shadow: 0 5px 15px rgba(0,0,0,0.1); }
        .form-group { margin-bottom: 20px; }
        .form-label { display: block; margin-bottom: 8px; font-weight: 500; color: #555; }
        .form-control { width: 100%; padding: 12px; border: 2px solid #e1e5e9; border-radius: 6px; font-size: 1rem; }
        .form-control:focus { outline: none; border-color: #667eea; }
        .btn { padding: 12px 24px; border: none; border-radius: 6px; cursor: pointer; font-size: 1rem; text-decoration: none; transition: background-color 0.3s ease; }
        .btn-primary { background: #667eea; color: white; }
        .btn-secondary { background: #6c757d; color: white; }
        .form-actions { display: flex; gap: 15px; justify-content: flex-end; margin-top: 30px; }
        .user-info { background: #f8f9fa; padding: 15px; border-radius: 6px; margin-bottom: 20px; border-left: 4px solid #667eea; }
    </style>
</head>
<body>
<div class="header">
    <div class="nav-bar">
        <div class="logo">📚 图书管理系统</div>
        <div class="admin-info">
            <span>欢迎，<strong><%= currentUser.getUsername() %></strong></span>
            <span class="admin-badge">管理员</span>
            <a href="<%= request.getContextPath() %>/logout" class="logout-btn">退出登录</a>
        </div>
    </div>
</div>

<div class="container">
    <div class="page-header">
        <h1 class="page-title">🔑 重置密码</h1>
        <a href="user-management" class="btn btn-secondary">返回用户列表</a>
    </div>

    <div class="user-info">
        <div><strong>用户ID:</strong> <%= userId %></div>
        <div><strong>用户名:</strong> <%= username %></div>
    </div>

    <div class="form-card">
        <form id="resetPasswordForm">
            <input type="hidden" name="userId" value="<%= userId %>">

            <div class="form-group">
                <label class="form-label" for="newPassword">新密码 *</label>
                <input type="password" id="newPassword" name="newPassword" class="form-control"
                       placeholder="请输入新密码（至少6位）" required minlength="6">
            </div>

            <div class="form-group">
                <label class="form-label" for="confirmPassword">确认密码 *</label>
                <input type="password" id="confirmPassword" name="confirmPassword" class="form-control"
                       placeholder="请再次输入新密码" required minlength="6">
            </div>

            <div class="form-actions">
                <button type="button" class="btn btn-secondary" onclick="window.history.back()">取消</button>
                <button type="submit" class="btn btn-primary">重置密码</button>
            </div>
        </form>
    </div>
</div>

<script>
    document.getElementById('resetPasswordForm').addEventListener('submit', function(e) {
        e.preventDefault();

        const formData = new FormData(this);
        const newPassword = document.getElementById('newPassword').value;
        const confirmPassword = document.getElementById('confirmPassword').value;

        if (newPassword !== confirmPassword) {
            alert('两次输入的密码不一致！');
            return;
        }

        if (newPassword.length < 6) {
            alert('密码长度至少6位！');
            return;
        }

        const submitBtn = this.querySelector('button[type="submit"]');
        submitBtn.textContent = '重置中...';
        submitBtn.disabled = true;

        fetch('<%= request.getContextPath() %>/admin/reset-password', {
            method: 'POST',
            body: new URLSearchParams(formData)
        })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('密码重置成功！');
                    window.location.href = 'user-management';
                } else {
                    alert('重置失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('重置失败，请检查网络连接');
            })
            .finally(() => {
                submitBtn.textContent = '重置密码';
                submitBtn.disabled = false;
            });
    });

    // 实时密码验证
    document.getElementById('confirmPassword').addEventListener('input', function() {
        const password = document.getElementById('newPassword').value;
        const confirmPassword = this.value;

        if (confirmPassword && password !== confirmPassword) {
            this.style.borderColor = '#dc3545';
        } else {
            this.style.borderColor = '#e1e5e9';
        }
    });
</script>
</body>
</html>
